package com.chii.order.service.impl;

import com.chii.order.mapper.OrderMapper;
import com.chii.order.po.Order;
import com.chii.order.service.OrderService;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.List;
import java.util.Random;

@Service
public class OrderServiceImpl implements OrderService {
    @Autowired
    private OrderMapper orderMapper;
    @Override
    public Order createOrder(Integer uid,String source) {
        DateTimeFormatter format = DateTimeFormatter.ofPattern("yyyyMMddHHmmssSSS");
        String nowStr = LocalDateTime.now().format(format);
        int ran = new Random().nextInt(8999)+1000;
        Order order = new Order();
        order.setOrderNo(nowStr+ran);
        order.setUid(uid);
        order.setOrderSource(source);
        order.setPaymentState(false);
        order.setPaymentAmount(10);
        order.setCreatedAt(new Date());

        orderMapper.insertSelective(order);
        return order;
    }

    @Override
    public int cancelOrder(Integer uid, String orderNo) {
        return 0;
    }

    @Override
    public Order getOrder(String orderNo) {
        return orderMapper.selectByOrderNo(orderNo);
    }

    @Override
    public List<Order> getOrderByUserId(Integer uid) {
        return orderMapper.selectByUid(uid);
    }

}
